/**
 * Created by Lxp on 16/4/5.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'card_cates',
                condition: {},
                model: {
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-status:click': 'status'
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var id = this.data.id;
                        var title = this.data.name;

                        this.data.act = 'edit';
                        var __this = this;

                        var html = template("add-tpl", this.data);
                        dialog({
                            'title': '编辑:' + title,
                            'content': html,
                            'width': 600,
                            ok: function () {
                                return _this.check_data(__this);
                            }, cancel: function () {
                            }
                        }).show();
                    },
                    status: function (e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'card_cate',
                            type: 'put',
                            data: {'id': _this.data.id, 'act': 'status'},
                            success: function (res) {
                                if (res.code == 0) {
                                    _this.data.status = _this.data.status == 1 ? 0 : 1;
                                    _this.create_element();
                                }
                            },
                            error: function () {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        check_data: function (__this) {
            var _this = this;
            var data = __this.data;

            data.name = $('[name=name]').val();
            if (data.name == '') {
                Alert({msg: '请输入名称', type: 'error'});
                return false;
            }
            data.listorder = $('[name=listorder]').val();
            if (data.listorder == '') {
                Alert({msg: '请输入排序', type: 'error'});
                return false;
            }
            if (data.id) {
                if (new Model({
                        url: _global.url.api + 'card_cate',
                        data: data
                    }).put()) {
                    __this.create_element();
                    return true;
                }
            } else {
                if (new Model({
                        url: _global.url.api + 'card_cate',
                        data: data
                    }).post()) {
                    _this.collection.reload();
                    return true;
                }
            }
            return false;
        },

        event: function () {
            this.data = {};
            var _this = this;
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                $('input[name="keyword"]').val('');
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
            //保存
            $(".btn-add").on("click", function (e) {
                e.preventDefault();
                var html = template("add-tpl", {});
                dialog({
                    'title': '添加分类',
                    'content': html,
                    'width': 600,
                    ok: function () {
                        return _this.check_data(_this);
                    }, cancel: function () {
                    }
                }).show();
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },
        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                $('#img-view').html(template('img-tpl', img));
            }
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});